import axios from 'axios'
import qs from 'qs'
import { MessageBox, Indicator } from 'mint-ui'

axios.interceptors.request.use(config => {
    Indicator.open({
        spinnerType: 'fading-circle'
    });
    return config
}, err => {
    return Promise.reject(err)
})

axios.interceptors.response.use(response => response, err => Promise.resolve(err.response))

function checkStatus(res) {
    console.log(res)
    console.log(123123)
    Indicator.close();
    if (res.status === 200 || res.status === 304) {
        return res
    }
    return {
        data: {
            code: 'N',
            message: res.statusText,
            data: res.statusText,
        }
    }
}

function checkCode(res) {
    if (res.data.code == 'L') {
        return 'L';
    }
    if (res.data.code !== 'Y') {
        var html = '操作失败';
        if (res.data.msg) {
            html = res.data.msg;
        }
        MessageBox('提示', html);
        return 'N';
    }
    return res.data.data
}
export default {
    get(url, params) {
        if (!url) return
        return axios({
            method: 'get',
            url,
            params,
            timeout: 30000
        }).then(checkStatus).then(checkCode)
    },
    post(url, data) {
        if (!url) return
        return axios({
            method: 'post',
            url,
            data: qs.stringify(data),
            headers: {  'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
            timeout: 30000
        }).then(checkStatus).then(checkCode)
    },
    postImg(url, data) {
        if (!url) return
        return axios({
            method: 'post',
            url,
            data: qs.stringify(data),
            headers: { 'Content-Type': 'multipart/mixed' },
            timeout: 30000
        }).then(checkStatus).then(checkCode)
    }
}
